/* 
 * File:   Sign.cpp
 * Author: Gab'z
 * 
 * Created on 4 juin 2012, 09:38
 */

#include "Sign.h"
#include "../../../Context.h"
#include "../../literal/Stack.h"
#include "../../literal/Complex.h"
#include "../../literal/Real.h"
#include "../../literal/Integer.h"

namespace calculator {

    Sign::Sign(const std::string name) : UnaryOperator(name) {
    }

    Sign::Sign(const Sign& orig) : UnaryOperator(orig._name) {
    }

    Sign::~Sign() {
    }

    std::string Sign::isExecutable() const {
        if (Context::getInstance()->getCurrentStack()->size() == 0) return "Empty stack";
        return "";
    }

    Sign* Sign::clone() const {
        return new Sign(_name);
    }

    const Number* Sign::apply(const Number* number) const throw (ArithmeticException) {
        const Number* n;
        const Integer* integer = new Integer(-1);
        try {
            n = mul(number, integer);
        } catch (ArithmeticException& e) {
            delete integer;
            throw;
        }
        delete integer;
        return n;
    }

}